package com.university.report.cmn.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.university.report.cmn.service.DictService;
import com.university.report.common.result.Result;
import com.university.report.common.result.ResultCodeEnum;
import com.university.report.model.entity.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Jinondo
 * @since 2021-06-10
 */
@Api(tags = "数据字典控制器")
@RestController
@RequestMapping("/api/dict")
public class DictController {

    @Autowired
    DictService dictService;

    @ApiOperation(value = "获取孩子")
    @GetMapping("/getChildren")
    public Result getChildrenById(Integer id){
        List<Dict> dicts = dictService.findChildren(id);
        Result<List> res = Result.ok(dicts);
        return res;
    }
    @ApiOperation(value = "通过name获取结点")
    @GetMapping("/getChildrenByName")
    public Result getChildrenByName(String name){
        QueryWrapper<Dict> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("name",name);
        Dict dict=dictService.getOne(queryWrapper);

        System.out.println(dict);
        List<Dict> dicts = dictService.findChildren(dict.getId());
        if (dicts!=null&&dicts.size()!=0)
        {
            return  Result.ok(dicts);
        }
        return Result.fail().message("获取数据失败！！").code(ResultCodeEnum.FAIL);
    }

}

